package logic

import (
	"context"
	"inventory-system/toryrpc/toryrpc"

	"inventory-system/api/internal/svc"
	"inventory-system/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateLogic) Update(req *types.UpdateRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	update, err := l.svcCtx.ToryRpc.ProductUpdate(l.ctx, &toryrpc.ProductUpdateRequest{
		ProductId: req.ProductId,
		Name:      req.Name,
		UserId:    req.UserId,
		Context:   req.Context,
		Price:     float32(req.Price),
		Image:     req.Image,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Data:    nil,
			Code:    0,
		}, nil
	}
	return &types.Response{
		Message: "修改成功",
		Data:    update,
		Code:    200,
	}, nil
}
